﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.IObjectBlls;
using CSP.Bll.CommonBll;
using CSP.Common.Factories;
using CSP.Model.CSP;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Dao;
using System.Collections.ObjectModel;

namespace CSP.Bll.ObjectBlls
{
    /// <summary>
    /// 年龄段
    /// </summary>
    public class AgeGroupBll : CSPCommonBll, IAgeGroupBll
    {
        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEYALL = "AgeGroupAll";

        private IAgeGroupDao _ageGroupDao;

        public AgeGroupBll()
        {
            _cacheProvider = CachedProviderFactory.GetLocalCachedProvider();
            _ageGroupDao = CSPDaoFactory.GetInstance().GetAgeGroupDao();
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <returns></returns>
        public ICollection<AgeGroup> FindAll()
        {
            //检测缓存是否存在
            ICollection<AgeGroup> ageGroups = _cacheProvider.GetData<Collection<AgeGroup>>(CACHEKEYALL);
            if (ageGroups == null)
            {
                //从数据库读取数据
                ageGroups = _ageGroupDao.FindAll();
                _cacheProvider.AddData(CACHEKEYALL, ageGroups, DateTime.Now.AddHours(10));
            }
            return ageGroups;
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AgeGroup GetDataById(int id)
        {
            AgeGroup ageGroup = GetById(id);
            if (ageGroup == null)
            {
                FindAll();
                ageGroup = GetById(id);
            }

            return ageGroup;
        }

        /// <summary>
        /// 根据Ids 获取数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public ICollection<AgeGroup> GeDatatByIds(int[] Ids)
        {
            ICollection<AgeGroup> ageGroups = GetByIds(Ids);
            if (ageGroups == null || ageGroups.Count == 0)
            {
                FindAll();
                ageGroups = GetByIds(Ids);
            }

            return ageGroups;
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="obj"></param>
        public void Add(AgeGroup obj)
        {
            if (obj == null)
            {
                throw new Exception("AgeGroup Can't be null");
            }
            _ageGroupDao.Add(obj);

            //清除缓存
            this.RefreshCache();

        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="obj"></param>
        public void Update(AgeGroup obj)
        {
            if (obj == null)
            {
                throw new Exception("AgeGroup Can't be null");
            }
            _ageGroupDao.Update(obj);

            //清除缓存0
            this.RefreshCache();

        }

        public void DeleteById(int id)
        {
            _ageGroupDao.DeleteById(id);
            
            //清除缓存0
            this.RefreshCache();
        }

        /// <summary>
        /// 刷新Cache
        /// </summary>
        public void RefreshCache()
        {
            //删除缓存Key
            _cacheProvider.Remove(CACHEKEYALL);
        }

        /// <summary>
        /// 通过Id 获取年龄范围
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private AgeGroup GetById(int id)
        {
            AgeGroup ageGroup = null;

            //检测缓存是否存在
            ICollection<AgeGroup> ageGroups = GetByIds(new int[] { id });
            if (ageGroups != null && ageGroups.Count != 0)
            {
                ageGroup = ageGroups.FirstOrDefault();
            }
            return ageGroup;
        }

        private ICollection<AgeGroup> GetByIds(int[] ids)
        {
            ICollection<AgeGroup> result = new Collection<AgeGroup>();

            //检测缓存是否存在
            ICollection<AgeGroup> ageGroups = _cacheProvider.GetData<Collection<AgeGroup>>(CACHEKEYALL);
            foreach (var item in ageGroups)
            {
                if (ids.Contains(item.id))
                {
                    result.Add(item);
                }
            }
            return result;
        }
    }
}
